import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreWechatMediaService extends BaseService<any> {
  private readonly logger = new Logger(CoreWechatMediaService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * addMedia
   * 对应 PHP: CoreWechatMediaService_core::addMedia()
   * 逻辑类型: undefined - undefined
   */
  async addMedia(data: any[]) {
    // 基于PHP真实逻辑: addMedia
    // PHP原文: $field = [             'media' => File::fromPath($data['file_path'])         ];         if ($data['type'] == WechatMediaDict::VIDEO) { //            $...
const field = [
            media: File.fromPath(data.file_path)
        ];
        if (data.type == WechatMediaDict.VIDEO) {
//            field.json = [
//                description: [
//                    title: time(),
//                    introduction: time()
//                ]
//            ];
            field.description = json_encode([title: time(), introduction: time()]];
  }

  /**
   * syncNewsMedia
   * 对应 PHP: CoreWechatMediaService_core::syncNewsMedia()
   * 逻辑类型: undefined - undefined
   */
  async syncNewsMedia(data: any[]) {
    // 基于PHP真实逻辑: syncNewsMedia
    // PHP原文: $options = [             'json' => [                 'offset' => $data['pages'],                 'count' => 20,                 'no_content' => 1     ...
const options = [
            json: [
                offset: data.pages,
                count: 20,
                no_content: 1
            ]
        ];
        const sync_res =  CoreWechatService.appApiClient(data.site_id).post("/cgi-bin/freepublish/batchget", options);
        if (typeof sync_res.errcode) && sync_res.errcode != 0) throw new BusinessException(sync_res.errmsg];

        const save_data = [);

        foreach (sync_res.item as item) {
            const media = this.model.where([ ['media_id', '=', item.article_id ) )).findOrEmpty(];
            if (media.isEmpty()) {
                save_data[] = [
                    site_id: data.site_id,
                    type: WechatMediaDict.NEWS,
                    value: json_encode(item.content),
                    media_id: item.article_id
                ];
            } } } else {
                media.value = json_encode(item.content];
                media.save();
            }
  }
}
